<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Product for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Product\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Product\Model\Product;
use Product\Form\ProductForm;
use Product\Form\DetailForm;

class ProductController extends AbstractActionController {
	protected $productTable;
	public function indexAction() {
		return new ViewModel ( array (
				'products' => $this->getHomeTable ()->fetchAll () 
		) );
	}
	public function addAction() {
		$form = new ProductForm ();
		$form->get ( 'submit' )->setValue ( 'Save' );
		$request = $this->getRequest ();
		
		if ($request->isPost ()) {
			$product = new Product ();
			$form->setInputFilter ( $product->getInputFilter () );
			$form->setData ( $request->getPost () );
			
			if ($form->isValid ()) {
				// echo 'valid';
				$product->exchangeArray ( $form->getData () );
				$this->getProductTable ()->saveProduct ( $product );
				
				return $this->redirect ()->toRoute ( 'product' );
			}
			// echo $product->name;
			// $testing = $form->getData();
			// echo $testing['name'];
		}
		return array (
				'form' => $form 
		);
	}
	public function editAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'product', array (
					'action' => 'add' 
			) );
		}
		
		// Get the Album with the specified id. An exception is thrown
		// if it cannot be found, in which case go to the index page.
		try {
			$product = $this->getProductTable ()->getProduct ( $id );
		} catch ( \Exception $ex ) {
			return $this->redirect ()->toRoute ( 'product', array (
					'action' => 'index' 
			) );
		}
		
		$form = new ProductForm ();
		$form->bind ( $product );
		$form->get ( 'submit' )->setAttribute ( 'value', 'Edit' );
		
		$request = $this->getRequest ();
		if ($request->isPost ()) {
			$form->setInputFilter ( $product->getInputFilter () );
			$form->setData ( $request->getPost () );
			
			if ($form->isValid ()) {
				$this->getProductTable ()->saveProduct ( $product );
				
				// Redirect to list of albums
				return $this->redirect ()->toRoute ( 'product' );
			}
		}
		
		return array (
				'id' => $id,
				'form' => $form 
		);
	}
	public function deleteAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'product' );
		}
		
		$request = $this->getRequest ();
		if ($request->isPost ()) {
			$del = $request->getPost ( 'del', 'No' );
			
			if ($del == 'Yes') {
				$id = ( int ) $request->getPost ( 'id' );
				$this->getProductTable ()->deleteProduct ( $id );
			}
			
			// Redirect to list of albums
			return $this->redirect ()->toRoute ( 'product' );
		}
		
		return array (
				'id' => $id,
				'product' => $this->getProductTable ()->getProduct ( $id ) 
		);
	}
	public function detailAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'product', array (
					'action' => 'index' 
			) );
		}
		
		// Get the Album with the specified id. An exception is thrown
		// if it cannot be found, in which case go to the index page.
		try {
			$product = $this->getDetailTable ()->getProduct ( $id );
		} catch ( \Exception $ex ) {
			return $this->redirect ()->toRoute ( 'product', array (
					'action' => 'index' 
			) );
		}
		
		$form = new DetailForm ();
		$form->bind ( $product );
		
		return array (
				'id' => $id,
				'form' => $form 
		);
	}
	public function fooAction() {
		// This shows the :controller and :action parameters in default route
		// are working when you browse to /product/product/foo
		return array ();
	}
	public function getHomeTable() {
		if (! $this->productTable) {
			$sm = $this->getServiceLocator ();
			$this->productTable = $sm->get ( 'Product\Model\HomeTable' );
		}
		return $this->productTable;
	}
	public function getProductTable() {
		if (! $this->productTable) {
			$sm = $this->getServiceLocator ();
			$this->productTable = $sm->get ( 'Product\Model\ProductTable' );
		}
		return $this->productTable;
	}
	public function getDetailTable() {
		if (! $this->productTable) {
			$sm = $this->getServiceLocator ();
			$this->productTable = $sm->get ( 'Product\Model\DetailTable' );
		}
		return $this->productTable;
	}
}
